package chase.myrewards.beans;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * Bean class that holds data for an account
 * 
 * @author Meena Abdou
 * @date April 21, 2013
 */
public class Account implements Serializable {

	/**
	 * This number must be unique for every class. And it can never change.
	 * You have to generate it automatically by clicking on the class name and
	 * then hitting Ctrl+1.  Then click on Add generated serial version ID. Then
	 * copy these comments on top of the generated id.
	 * 
	 * @serial serialVersionUID
	 */
	private static final long serialVersionUID = -6586431686705486852L;

	private String accountNum, accountType, maskedAccountNum;
	private int previousBalance, currentBalance, futureBalance;
	private int usedPoints, recentlyAccrued, cycleNum;
	private String cycleDate;
	private ArrayList<String> programID;
	private ArrayList<String> redeemNum;
	private ArrayList<String> redeemUrl;
	private ArrayList<String> redeemStore;
	private ArrayList<String> redeemCard;
	// Added to be used by Location module
	//private ArrayList<String> promoID;

	private int size;

	public Account(){
		this.accountNum = "0";
		this.accountType = "0";
		this.maskedAccountNum = "0";
		this.previousBalance = 0;
		this.currentBalance = 0;
		this.futureBalance = 0;
		this.usedPoints = 0;
		this.recentlyAccrued = 0;
		this.cycleNum = 0;
		this.cycleDate = "0";
		programID = new ArrayList<String>();
		redeemNum = new ArrayList<String>();
		redeemUrl = new ArrayList<String>();
		redeemStore = new ArrayList<String>();
		redeemCard = new ArrayList<String>();
		size = 0;
	}
	
	public String toString(){
		StringBuffer sbuff = new StringBuffer(70);
		sbuff.append("\n");
		sbuff.append("Account Number: " + accountNum);
		sbuff.append("\n");
		sbuff.append("Account Type: " + accountType);
		sbuff.append("\n");
		sbuff.append("Cycle Num: " + cycleNum);
		sbuff.append("\n");
		return sbuff.toString();
	}

	public String getAccountNum() {
		return accountNum;
	}

	public void setAccountNum(String accountNum) {
		this.accountNum = accountNum;
		maskAccountNum();
	}

	public void setMaskedAccountNum(String maskedAccountNum) {
		this.maskedAccountNum = maskedAccountNum;
	}

	public String getMaskedAccountNum() {
		return maskedAccountNum;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public int getPreviousBalance() {
		return previousBalance;
	}

	public void setPreviousBalance(int previousBalance) {
		this.previousBalance = previousBalance;
	}

	public int getCurrentBalance() {
		return currentBalance;
	}

	public void setCurrentBalance(int currentBalance) {
		this.currentBalance = currentBalance;
	}

	public int getFutureBalance() {
		return futureBalance;
	}

	public void setFutureBalance(int futureBalance) {
		this.futureBalance = futureBalance;
	}

	public int getUsedPoints() {
		return usedPoints;
	}

	public void setUsedPoints(int usedPoints) {
		this.usedPoints = usedPoints;
	}

	public int getRecentlyAccrued() {
		return recentlyAccrued;
	}

	public void setRecentlyAccrued(int recentlyAccrued) {
		this.recentlyAccrued = recentlyAccrued;
	}

	public int getCycleNum() {
		return cycleNum;
	}

	public void setCycleNum(int cycleNum) {
		this.cycleNum = cycleNum;
		cycleNumToDate();
	}

	public void insertChannels(String program, String redeemnum, String redeemurl, String redeemstore, String redeemcard){
		this.programID.add(program);
		this.redeemNum.add(redeemnum);
		this.redeemUrl.add(redeemurl);
		this.redeemStore.add(redeemstore);
		this.redeemCard.add(redeemcard);
		setSize(programID.size());
	}

	public void insertProgramID(String program){
		this.programID.add(program);
	}

	public void insertRedeemNum(String redeemnum){
		this.redeemNum.add(redeemnum);
	}

	public void insertRedeemUrl(String redeemurl){
		this.redeemUrl.add(redeemurl);
	}

	public void insertRedeemStore(String redeemstore){
		this.redeemStore.add(redeemstore);
	}

	public void insertRedeemCard(String redeemcard){
		this.redeemCard.add(redeemcard);
	}
	
	public ArrayList<String> getProgramID() {
		return programID;
	}

	public ArrayList<String> getRedeemNum() {
		return redeemNum;
	}

	public ArrayList<String> getRedeemUrl() {
		return redeemUrl;
	}
	
	public ArrayList<String> getRedeemStore() {
		return redeemStore;
	}

	public ArrayList<String> getRedeemCard() {
		return redeemCard;
	}

	public void setProgramID(ArrayList<String> programID) {
		this.programID = programID;
	}

	public void setRedeemNum(ArrayList<String> redeemNum) {
		this.redeemNum = redeemNum;
	}

	public void setRedeemUrl(ArrayList<String> redeemUrl) {
		this.redeemUrl = redeemUrl;
	}

	public void setRedeemStore(ArrayList<String> redeemStore) {
		this.redeemStore = redeemStore;
	}

	public void setRedeemCard(ArrayList<String> redeemCard) {
		this.redeemCard = redeemCard;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getSize() {
		return size;
	}	

	public void maskAccountNum(){
		int total = accountNum.length(); // total length of account number
		int endlen = 4;   // length of unmasked chars from end of string
		int masklen = total - endlen;  // length of masked characters

		// if the string is less than endlen chars, then don't mask anything
		if (masklen < 0){
			masklen = 0;
			maskedAccountNum = accountNum;
			return;
		}

		// initialize string buffer starting with unmasked starting chars
		StringBuffer maskedbuf = new StringBuffer();

		// mask the next masklen characters with X 
		for(int i = 0; i < masklen; i++) {
			maskedbuf.append('X');
		}
		maskedbuf.append('-');

		// append the remaining unmasked characters
		maskedbuf.append(accountNum.substring(masklen, total));
		String masked = maskedbuf.toString();
		maskedAccountNum = masked;
	}

	public void setCycleDate(String cycleDate) {
		this.cycleDate = cycleDate;
	}

	public String getCycleDate() {
		return cycleDate;
	}
	
	public void cycleNumToDate(){
		DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		Date date = new Date();
		if (date.getDate() > cycleNum){
			date.setMonth(date.getMonth()+1);
		}
		date.setDate(cycleNum);
		cycleDate = dateFormat.format(date);
	}
}
